package com.caigou.modules.login.ui

import android.os.Bundle
import android.text.InputType
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.bean.Session
import com.caigou.comm.base.bean.UserBean
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.DialogUtil
import com.caigou.comm.base.utils.SessionUtil
import com.caigou.modules.login.R
import com.caigou.modules.login.databinding.FragmentLauncherBinding
import com.caigou.modules.login.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/09/22
 */
@AndroidEntryPoint
class LauncherFragment : BaseFragment(), View.OnClickListener {

    private var _binding: FragmentLauncherBinding? = null
    private val binding get() = _binding!!
    private var isShowText = false

    private val loginViewModel: LoginViewModel by viewModels()

    override val mDialog by lazy {
        DialogUtil.getLoadingDialog(requireActivity(), "登录中...")
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentLauncherBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun onBack() {

    }

    override fun initView() {
        binding.launcherIvIcon.setImageResource(R.drawable.img_comm_icon)
        binding.launcherEetMobile.run {
            leftImageView.setImageResource(R.drawable.img_login_account)
            editText.hint = "手机号"
        }
        binding.launcherEetPwd.run {
            leftImageView.setImageResource(R.drawable.img_login_pwd)
            editText.hint = "密码"
            rightImageView.setImageResource(R.drawable.img_login_eye1)
            rightImageView.setOnClickListener {
                if (isShowText) {
                    isShowText = false
                    rightImageView.setImageResource(R.drawable.img_login_eye1)
                    editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
                } else {
                    isShowText = true
                    rightImageView.setImageResource(R.drawable.img_login_eye2)
                    editText.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                }
            }
            editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
        }

        binding.launcherTvForget.setOnClickListener {
            (requireActivity() as LoginActivity).switchFragment(RouterPath.Login.TAG_FORGET)
        }

        binding.launcherBtnRegister.setOnClickListener {
            (requireActivity() as LoginActivity).switchFragment(RouterPath.Login.TAG_VERIFY)
        }
        binding.launcherBtnLauncher.setOnClickListener(this)
    }

    override fun lazyLoad() {
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(loginViewModel.userLiveData, ::handleLauncher)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleLauncher(status: Resource<UserBean>) {
        when (status) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(status.errorMsg)) {
                    showToast(status.errorMsg)
                } else {
                    showToast("用户登录异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                if (status.data == null) {
                    return
                }

                val mobile = binding.launcherEetMobile.editText.text.toString()
                val password = binding.launcherEetPwd.editText.text.toString()
                val session = Session(mobile, password)
                SessionUtil.setSession(session)

                AppBackgroundInfo.instance.hasLogin = true
                AppBackgroundInfo.instance.userBean = status.data
                LiveDataBus.get().with(BusKey.LOGIN_SUCCESS).value = Any()
                requireActivity().finish()
            }
        }
    }

    override fun onClick(v: View?) {
        v?.apply {
            when (id) {
                binding.launcherBtnLauncher.id -> {
                    val mobile = binding.launcherEetMobile.editText.text.toString()
                    val pwd = binding.launcherEetPwd.editText.text.toString()
                    if (TextUtils.isEmpty(mobile)) {
                        showToast("手机号不能为空")
                        return
                    }
                    if (TextUtils.isEmpty(pwd)) {
                        showToast("密码不能为空")
                        return
                    }

                    loginViewModel.launcher(mobile, pwd)
                }
            }
        }
    }
}